package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.Flower;
import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.service.FlowerService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 黄军艳
 * @version 1.0
 * @date 2024/12/23  11:01
 */
@Controller
@RequestMapping("/main/flower")
public class FlowerController extends BaseController {
    private static final Logger LOGGER =
            LoggerFactory.getLogger(StorageRecordController.class);

    @Autowired
    private FlowerService flowerService;

    @RequestMapping("/page")
    public String page(Model model, Flower condition,
                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        Page<Flower> page=null;
        try {
            page=flowerService.pageList(condition,pageNo,pageSize);
            model.addAttribute("condition",condition);
            model.addAttribute("pageInfo",page);
        } catch (Exception e) {
            LOGGER.error("FlowerController 中的 page 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "flower/page";
    }

    //跳转到添加页面
    @RequestMapping("/save")
    public String tosave(Model model){
        try {
            model.addAttribute("flower",new Flower());
        }catch (Exception e) {
            LOGGER.error("FlowerController 中的 save 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "flower/save";
    }

    @PostMapping("/save")
    public String dosave(Flower flower){
        try {
            boolean ok=false;
            if (flower.getFlowerId()==null){
                ok=flowerService.insert(flower);
            }else {
                ok=flowerService.update(flower);
            }
            if (ok){
                return redirect("/main/flower/page");
            }
        }catch (Exception e) {
            LOGGER.error("FlowerController 中的 dosave 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "flower/save";
    }

    @GetMapping("/edit")
    public String edit(Model model,@RequestParam("id") Integer id){
        Flower flower=null;
        try {
            flower=flowerService.selectId(id);
            model.addAttribute("flower",flower);
        }catch (Exception e) {
            LOGGER.error("FlowerController 中的 edit 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "flower/save";
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object delete(@PathVariable("id") Integer id){
        Map<String,Object> map=new HashMap<>();
        try {
            boolean ok=flowerService.delete(id);
            if (ok){
                map.put("code",200);
            }else {
                map.put("code",500);
                map.put("msg","删除失败");
            }
        }catch (Exception e) {
            LOGGER.error("FlowerController 中的 delete 方法执行发生错误。 ", e);
            map.put("code",500);
            map.put("msg","删除失败");
        }
        return map;
    }

    //查重
    @GetMapping("/flowerNameExist")
    @ResponseBody
    public Object flowerNameExist(@RequestParam("flowerName") String flowerName){
        Map<String,Object> map=new HashMap<>();
        try {
            Flower flower=flowerService.selectName(flowerName);
            if (flower!=null){
                map.put("exist",1);
            }else {
                map.put("exist",0);
            }
        }catch (Exception e) {
            LOGGER.error("FlowerController 中的 flowerNameExist 方法执行发生错误。 ", e);
            map.put("exist",-1);
            map.put("error","系统繁忙，请稍后再试！");
        }
        return map;
    }

    //详情
    @GetMapping("/view/{id}")
    public String view(Model model,@PathVariable("id") Integer id){
        Flower flower=null;
        try {
            flower=flowerService.selectId(id);
            model.addAttribute("flower",flower);
        }catch (Exception e) {
            LOGGER.error("FlowerController 中的 view 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "flower/view";
    }
}
